<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Music extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['music_addtime','music_sid','music_uid'];
	protected $update = [];	
	public $fieldlist = [1=>'主题曲',2=>'插曲',3=>'片尾曲',4=>'片头曲'];
    public function getTypeTxtAttr($value){
        $txt = $this->$fieldlist;
        return $txt[$value];
    }	
	protected function setMusicAddtimeAttr($value,$data){
		if (!empty($data['music_checktime']) || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}	
	protected function setMusicSidAttr($value){
		return 1;
	}
	protected function setMusicUidAttr($value){
		if($value){
			return $value;
		}else{
			return '';
		}
	}	
	//事件注册
    protected static function init(){
		//写入后置事件
		music::afterWrite(function ($music){

		});		
		//删除后置操作
        music::afterDelete(function ($music) {
       });	
    }		
    public function vod(){
        return $this->hasOne('vod','vod_id','music_vid');
    }
    public function role(){
        return $this->hasOne('role','role_id','music_rid');
    }
    public function user(){
        return $this->hasOne('user','user_id','music_uid');
    }
	//数据查询
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='music_'.$param['order'].' desc';
		}
		if(empty(order_check($tag['order']))){
			$tag['order']='music_addtime desc';
		}		
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		if(empty($tag['status'])) {
		        $where['music_status']=['eq',1];
		}else{
			if($tag['status']==2){
			   $where['music_status']=['eq',0];
			}
		} 
	    if(!empty($tag['ids'])){
		     $where['music_id'] = array('in',$tag['ids']);
	    }		
	    if(!empty($tag['not'])){
		     $where['music_id'] = array('not in',$tag['not']);
	    }	
	    if (!empty($tag['vid'])) {
		    $vid = explode(',',$tag['vid']);
		    if(count($vid)>1) {
			    $where['music_vid'] = array('in',$tag['vid']);
		    }else{
			    $where['music_vid'] = array('eq',$vid[0]);
		    }
	    }
	    if (!empty($tag['star'])) {
		    $star = explode(',',$tag['star']);
		    if(count($star)>1) {
			    $where['music_star'] = array('in',$tag['star']);
		    }else{
			    $where['music_star'] = array('eq',$star[0]);
		    }
	    }								
	    if (!empty($tag['hits'])) {
		    $hits = explode(',',$tag['hits']);
		    if (count($hits) > 1) {
			    $where['music_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['music_hits'] = array('gt',$hits[0]);
		    }
	    }		
	    if(!empty($tag['up'])) {
		    $up = explode(',',$tag['up']);
		    if(count($up)>1) {
			    $where['music_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['music_up'] = array('gt',$up[0]);
		    }
	    }		
	    if(!empty($tag['down'])) {
		    $down = explode(',',$tag['down']);
		    if(count($down)>1) {
			    $where['music_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['music_down'] = array('gt',$down[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['music_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['music_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }
	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['music_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['music_uid'] = array('eq',$uids[0]);
		    }
			$rs=db('music')->alias('music')->join('vod vod','vod.vod_id = music_vid','LEFT')->join('star star','star.star_name = music_star','LEFT');
	    }else{
			if(!empty($tag['vod'])){
			    $rs=db('music')->alias('music')->join('vod vod','vod.vod_id = music_vid','LEFT')->join('star star','star.star_name = music_star','LEFT');
			}else{
				$rs=db('music')->alias('music')->join('star star','star.star_name = music_star','LEFT');
			}
			
		}		
        if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = $rs->where($where)->field($tag['field'])->group($tag['group'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = $rs->where($where)->field($tag['field'])->group($tag['group'])->order($tag['order'])->limit($limit_str)->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_music')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 	
}